@extends("admin.frame_layout")
<?php $top_selected = "信息管理";$page_menu = "系统公告" ?>
@section('content')
    <div class="formbody">
        <div class="formtitle"><span>系统公告编辑</span></div>

        <form action="{{route('admin.notices.save',['id'=>isset($notices)?($notices->id):(0)])}}"
              method="post" id="notices">
            {!! csrf_field() !!}

            <ul class="forminfo">

                {!! inputText("公告标题","title","text",
                    isset($notices)?$notices->title:"",
                    "标题建议最长不超过50个字符！") !!}

                <li>
                    <label class="lable">开始时间</label>
                    <input id="start" name="start" class="dfinput" type="text"
                           value="{{isset($notices)?(date('Y-m-d H:i:s',$notices->start)):""}}"
                           onClick="laydate({istime: true,format: 'YYYY-MM-DD hh:mm:ss'})">
                    <i for="start">公告生效时间！</i>
                </li>

                <li>
                    <label class="lable">结束时间</label>
                    <input id="end" name="end" class="dfinput" type="text"
                           value="{{isset($notices)?(date('Y-m-d H:i:s',$notices->end)):""}}"
                           onClick="laydate({istime: true,format: 'YYYY-MM-DD hh:mm:ss'})">
                    <i for="end">公告过期时间！</i>
                </li>

                <li>
                    <label class="lable">公告内容</label>
                    {{-- 加载编辑器的容器 --}}
                    <div style="float: left;width: 800px;">
                        <script name="content" id="content"
                                type="text/plain">{!! $notices->content or "" !!}</script>
                    </div>
                    <div style="clear: both;"></div>
                </li>
                <li><label class="lable">&nbsp;</label>
                    <input type="submit" class="btn" value="确认保存"/>
                </li>
            </ul>
        </form>
    </div>
@endsection

@section('footer')
    <script src="{{asset('admin/js/ueditor/ueditor.config.js')}}"></script>
    <script src="{{asset('admin/js/ueditor/ueditor.all.js')}}"></script>
    <script src="{{asset('admin/js/laydate/laydate.js')}}"></script>
    {{-- 实例化编辑器 --}}
    <script type="text/javascript">
        $(function () {
            laydate.skin("dahong"); //大红色日期选择皮肤

            //初始化编辑器
            var ue = UE.getEditor('content', {
                initialFrameHeight: 250
            });

            $("#notices").validate({
                rules: {
                    title: {
                        required: true,
                        maxlength: 100
                    },
                    start: {required: true},
                    end: {required: true}
                },
                errorPlacement: function (error, element) {
                    $(element).closest("form").find("i[for='" + element.attr("id") + "']").html(error);
                },
                submitHandler: function (form) {
                    if (ue.hasContents()) {
                        ajaxSub(form);
                    } else {
                        toastr["error"]("请填写公告详情！");
                    }
                }
            });

            /**
             * ajax提交请求
             */
            function ajaxSub(form) {
                $(form).ajaxSubmit({
                    dataType: "json",
                    type: "post",
                    success: function showSuccess(data) {
                        if (data.code == 0) {
                            toastr["success"](data.msg);
                            setInterval(function () {
                                window.location = "{{route('admin.notices')}}";
                            }, 2000);
                        } else {
                            toastr["error"](data.msg);
                        }
                    }
                });
            }
        });
    </script>
@endsection
